package com.tju.resource.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tju.resource.mapper.TbProcessDepMapper;
import com.tju.resource.mapper.TbProcessMapper;
import com.tju.resource.model.TbProcess;
import com.tju.resource.model.TbProcessDep;
import com.tju.resource.server.TbProcessService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import com.tju.resource.tool.tool.ChineseExceptionUtils;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.tool.tool.EmptyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 工序信息管理数据接口
 */
@Slf4j
@Service
public class TbProcessServiceImpl implements TbProcessService {

    @Autowired
    private TbProcessMapper tbProcessMapper;
    @Autowired
    private TbProcessDepMapper tbProcessDepMapper;

    @Autowired
    private ChineseExceptionUtils zh_CH;

    /**
     * 新增单工序管理信息
     *
     * @param tbProcess
     * @return
     */
    @Transactional
    @Override
    public Result<Object> addTbProcess(TbProcess tbProcess) {
        try {
            QueryWrapper<TbProcess> wrapper = new QueryWrapper<>();
            wrapper.eq("process_name", tbProcess.getProcessName());
            if (EmptyUtils.isEmpty(tbProcessMapper.selectOne(wrapper))) {
                if (tbProcessMapper.insert(tbProcess) > 0) {
                    return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.AddedSuccessfully);
                }
            } else
                return ResultUtil.error(ResultEnum.SUCCESS, zh_CH.ProcessExisted);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResultUtil.error(ResultEnum.SUCCESS, zh_CH.AddFailed);
    }

    /**
     * 修改单工序管理信息
     *
     * @param tbProcess
     * @return
     */
    @Transactional
    @Override
    public Result<Object> modifyTbProcess(TbProcess tbProcess) {
        try {
            QueryWrapper<TbProcess> wrapper = new QueryWrapper<>();
            wrapper.eq("process_name", tbProcess.getProcessName());
            TbProcess process = tbProcessMapper.selectOne(wrapper);
            if (EmptyUtils.isNotEmpty(process))
                if (!process.getId().equals(tbProcess.getId()))
                    return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.ProcessExisted);
            if (tbProcessMapper.updateById(tbProcess) > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.SuccessfullyModified);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.FailToEdit);
    }

    /**
     * 删除单工序管理信息
     *
     * @param tbProcess
     * @return
     */
    @Transactional
    @Override
    public Result<Object> deleteTbProcess(TbProcess tbProcess) {
        try {
            if (EmptyUtils.isNotEmpty(tbProcess.getId())) {
                QueryWrapper<TbProcessDep> wrapper = new QueryWrapper<>();
                wrapper.eq("process_id", tbProcess.getId()).last("limit 1");
                if (EmptyUtils.isEmpty(tbProcessDepMapper.selectOne(wrapper))) {
                    if (tbProcessMapper.deleteById(tbProcess.getId()) > 0)
                        return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.SuccessfullyDeleted);
                } else
                    return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.ProcessIsAlreadyOccupied);
            } else
                return ResultUtil.error(ResultEnum.SUCCESS, zh_CH.MissingParameters);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResultUtil.error(ResultEnum.SUCCESS, zh_CH.FailedToDelete);
    }

    /**
     * 分页查询全部单工序管理信息
     *
     * @param parameter
     * @return
     */
    @Override
    public Result<Object> getPageTbProcess(ClientParameters parameter) {
        try {
            if (EmptyUtils.isNotEmpty(parameter.getPage()) && EmptyUtils.isNotEmpty(parameter.getLimit())) {
                QueryWrapper<TbProcess> wrapper = new QueryWrapper<>();
                wrapper.orderByDesc("id");
                if (EmptyUtils.isNotEmpty(parameter.getOne())) {
                    wrapper.like("process_name", parameter.getOne());
                }
                if (EmptyUtils.isNotEmpty(parameter.getPage()) && EmptyUtils.isNotEmpty(parameter.getLimit())) {
                    Page<TbProcess> page = new Page<>(parameter.getPage(), parameter.getLimit());
                    IPage<TbProcess> iPage = tbProcessMapper.selectPage(page, wrapper);
                    return ResultUtil.success(ResultEnum.SUCCESS, iPage.getRecords(), iPage.getTotal());
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    /**
     * 查询全部工序
     *
     * @return
     */
    @Override
    public Result<Object> getTbProcess() {
        try {
            return ResultUtil.success(ResultEnum.SUCCESS, tbProcessMapper.selectList(null));
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }
}
